package com.cloud.controller.good;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.bean.good.Good;
import com.cloud.bean.menu.Menu;
import com.cloud.bean.user.User;
import com.cloud.common.BaseReturn;
import com.cloud.service.good.GoodService;
import com.cloud.service.menu.MenuService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

import static com.cloud.common.JwtUtils.getByToken;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liuyuan
 * @since 2022-07-26
 */
@RestController
@RequestMapping("/good")
public class GoodController {
    @Autowired
    GoodService goodService;
    @Autowired
    MenuService menuService;


    @RequestMapping("/queryList")
    public BaseReturn  queryList(@RequestBody GoodForm goodForm){
        User user = getByToken();
        if(user == null){
            return BaseReturn.build("4003","用户异常请你联系管理员");
        }
        goodForm.setCustomerNo(user.getCustomerNo());
        QueryWrapper<Good> goodQueryWrapper = new QueryWrapper<>(goodForm);
        if(goodForm.getPage() == null){
            goodForm.setPage(1);
        }
        if(goodForm.getSize() == null){
            goodForm.setSize(5);
        }
        Page<Good> page = new Page<>(goodForm.getPage(),goodForm.getSize());
        IPage<Good> pageList = goodService.page(page, goodQueryWrapper);
        List<Good> records = pageList.getRecords();
        List<GoodData> goodDatas = new ArrayList<>();
        for(int i=0;i<records.size();i++){
            Good good = records.get(i);
            GoodData goodData = new GoodData();
            BeanUtils.copyProperties(good,goodData);
            Long menuId = goodData.getMenuId();
            if(menuId != null){
                Menu menu = menuService.getById(menuId);
                goodData.setMenu(menu);
            }
            goodDatas.add(goodData);
        }
        Page<GoodData> goodDataPage = new Page<>();
        BeanUtils.copyProperties(pageList,goodDataPage);
        goodDataPage.setRecords(goodDatas);
        return BaseReturn.build("2000","查询成功",goodDataPage);
    }


    @RequestMapping("/insert")
    public BaseReturn insert(@RequestBody Good good){
        User user = getByToken();
        if(user == null){
            return BaseReturn.build("4003","登录失效请你重新登录");
        }
        String customerNo = user.getCustomerNo();
        if(customerNo == null){
            return BaseReturn.build("5000","请你联系管理员,绑定商户号");
        }
        good.setCustomerNo(customerNo);
        if(good.getName() == null){
            return BaseReturn.build("5000","商品名称为必填项");
        }
        if(good.getSellPrice() == null){
            return BaseReturn.build("5000","售价为必填项");
        }
        if(good.getCostPrice() == null){
            return BaseReturn.build("5000","成本价为必填项");
        }
        good.setNum(0);
        boolean save = goodService.save(good);
        if(save){
           return BaseReturn.build("2000","添加成功");
        }else {
           return BaseReturn.build("5000","添加失败");
        }

    }

    @RequestMapping("/updateById")
    public BaseReturn updateById(@RequestBody Good good){
        boolean b = goodService.updateById(good);
        if(b){
            return BaseReturn.build("2000","修改成功");
        }else {
            return BaseReturn.build("5000","修改失败");
        }
    }
    @RequestMapping("/getById")
    public BaseReturn getById( Long id){
        Good byId = goodService.getById(id);
        if(byId !=null){
            return BaseReturn.build("2000","查询成功",byId);
        }else {
            return BaseReturn.build("5000","暂无数据");
        }
    }
}

